package com.sy.easycms.web;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.sy.easycms.bo.ContentBO;
import com.sy.easycms.orm.Content;

/**
 * 
 * @author Luke Sun
 * @version 1.0
 */
public class IndexAction extends Action {

	private ContentBO dao = new ContentBO();
	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String page = request.getParameter("page"); //页码
		int p = 1; //默认页码
		if(page == null || page == "")
		{
			//p保持默认值
		}else
		{
			try{
				p = Integer.valueOf(page);
			}catch(NumberFormatException nfe)
			{
				//参数不正确
				p = 1;
			}
		}
		try{
			List<Content> content = this.dao.selectArticleByPage(p);
			List<String> pagination = this.dao.getPage();
			request.setAttribute("Article", content);
			request.setAttribute("Pagination", pagination);
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return mapping.findForward("IndexPage");
	}
}